<?php 
namespace app\supermanager\model;
use app\common\traits\model\TraitModel;
use Carbon\Carbon;
/**
 * 总公司退押金模型
 */
class QuitDeposits extends \think\Model
{
	use TraitModel;
	protected $table = 'quit_deposit';
	const ORDER_REFUND_ON = 102;//退款中
	const ORDER_CANCEL_STATUS = 103;//取消订单
	const ORDER_REFUND_DEPOSIT_STATUS = 202;//返还押金中
    const ORDER_REFUND_FINISH = 203;//已完成的订单
    //订单来源
	const ORDERSOURCE = 3;//心愿订单
	const ORDERRENTS = 2;//出租订单
	const ORDERSHARES = 1;//交换订单
	/**
     * 获取退款订单列表
     * @param array $query 查询条件
     * @return [type] [description]
     */
    public function getOrders($query = [],$order="created_at desc")
    {
        return $this->getInfoList($query,$order);
    }
	
	/**
	 * 退款
	 * @param $where 
	 * 
	 */
	public function quitDeposit($where){
		//查看订单是否存在
		$data = $this->where($where)->find();		
		if(!$data){
			$this->error = '订单不存在';
			return false;
		}
		$this->startTrans();
		try {
			//取消订单退款
			if($data->status == self::ORDER_REFUND_ON){
				$this->addIntergrates(model('Orders')->get(['number' => $data->number]),$data->intergrate);
				$this->where(['number'=>$data->number])->update(['status'=>self::ORDER_CANCEL_STATUS]);
				model('Orders')->where(['number'=>$data->number])->update(['status'=>self::ORDER_CANCEL_STATUS]);
			}
			//离开订单返回押金
			if($data->status == self::ORDER_REFUND_DEPOSIT_STATUS){
				$this->where(['number'=>$data->number])->update(['status'=>self::ORDER_REFUND_FINISH]);
				model('Orders')->where(['number'=>$data->number])->update(['status'=>self::ORDER_REFUND_FINISH]);
				$order = model('Orders')->get(['number' => $data->number]);
				
				//出租成功更新车主账户余额
				$user_id = $order->caravan_user_id;
				$total_money = $order->$total_money;
				if ($order->getData('source') === self::ORDERRENTS) {
					model('Capitals')->where('user_id',$user_id)->update(['amount'=>$this->addCash($user_id,$total_money)]);
				}
				//分享成功后获得旅行币
				$remark = '房车分享';
				if ($order->getData('source') === self::ORDERSHARES) {
					model('Intergrates')->reduceIntergrates($this->reduceIntergrtates($caravan_id, $start_date, $end_date), $user_id = null, ['remark' =>$remark]);
				}
				//接单用户获得旅行币和悬赏金额
				if($order->getData('source') === self::ORDERSOURCE){
					model('Intergrates')->addIntergrates($order->total_intergral,$order->caravan_user_id);
					model('Capitals')->incCapital($order->total_money,$order->caravan_user_id);
				}
			}
			$this->commit();
		} catch (Exception $e) {
		    $this->rollback();
			$this->error = '操作异常';
			return false;
		}

		$this->notifys($data->user_id, $data->number);

		return true;
	} 
	public function addIntergrates($order, $intergrate = null)
	{
		$intergrate = ($intergrate !== null || $intergrate === 0) ? $intergrate : $order->total_intergral;
		model('Intergrates')->addIntergrates($intergrate, $order->rent_user_id);
	}
	
	/**
	 * 发送通知信息
	 * @param  [type] $to_userid 发送的用户id
	 * @param  [type] $order_id  订单id
	 * @return [type]            [description]
	 */
	protected function notifys($to_userid, $order_id)
    {
    	if ($open_id = model('Users')->where(['id' => $to_userid, 'oauth_type' => 'wx'])->value('open_id')) {
    		$order = $this->get(['number' => $order_id, 'user_id' => $to_userid]);

    		$notice = new \app\common\Notice();
	    	$data = array(
	                 "first"  => '您好！退款到账啦',
	                 "keyword1"   => $order->number,
	                 "keyword2"  => "双辉旅程网",
	                 "keyword3"  => "现金:" . $order->deposit . "元,积分:" . $order->intergrate,
	                 "keyword4"  => "订单押金",
	                 "keyword5"  => Carbon::now()->toDateTimeString(),
	                 "remark" => "",
	                );
	    	$notice->setTemplate('quit_deposits')->send($open_id, $data);
    	}
        model('Messages')->createMsg(['users_id' => $to_userid, 'body' => '@quitdeposits', 'vars' => [$order_id, $order_id]]);
    }

    public function addCash($user_id,$total_money)
    {
    	$cash = model('Capitals')->where('user_id',$user_id)->value('amount');
    	return $all_cash = $cash + $total_money;
    }
}

 ?>